import type { ComponentResolver } from 'unplugin-vue-components/types';
import type { PluginOption } from 'vite';

import {
  AntDesignVueResolver,
  ArcoResolver,
  ElementPlusResolver,
  NaiveUiResolver,
} from 'unplugin-vue-components/resolvers';
import components from 'unplugin-vue-components/vite';

type UI_TYPE = 'ant-design' | 'arco-design' | 'element-plus' | 'naive-ui';

export function AutoImportComponents(
  options = {},
  ui: UI_TYPE | UI_TYPE[],
): PluginOption {
  const resolvers = UI_RESOLVERS(ui);
  return components({
    ...options,
    resolvers,
  });
}

const UI_RESOLVERS = (ui: UI_TYPE | UI_TYPE[]) => {
  const inputUIs = Array.isArray(ui) ? ui : [ui];
  let resolvers: ComponentResolver[] = [];

  inputUIs.forEach((singleUI) => {
    switch (singleUI) {
      case 'ant-design': {
        resolvers.push(AntDesignVueResolver());
        break;
      }
      case 'arco-design': {
        resolvers.push(ArcoResolver());
        break;
      }
      case 'element-plus': {
        resolvers = [...resolvers, ...ElementPlusResolver()];
        break;
      }
      case 'naive-ui': {
        resolvers.push(NaiveUiResolver());
        break;
      }
      default: {
        console.error(`Unsupported UI type: ${singleUI}`);
      }
    }
  });

  return resolvers;
};
